package cn.com.ddapp.ddm.fragment.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.LandsortItemAdpter;
import cn.com.ddapp.ddm.adapter.SlidingBigAdpter;
import cn.com.ddapp.ddm.adapter.SlidingSmallAdpter;
import cn.com.ddapp.ddm.adapter.SlidinghairAdpter;
import cn.com.ddapp.ddm.adapter.SlidingsiteAdpter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.SlidingSmallBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import okhttp3.Call;

/**
 * @FileName FitrerFragment
 * @Description 商品分类一级筛选界面
 */
public class FitrerFragment extends SlidingFragment implements  View.OnClickListener{

    ListView goodsort_listview;
    DrawerLayout drawer_layout;
    RelativeLayout drawer_content;

    private TextView goodsort_reset;
    private TextView goodsort_sure;
    private SlidingBigAdpter bigAdpter;
    private SlidingSmallAdpter smallAdpter;
    private SlidingsiteAdpter siteAdapter;
    private SlidinghairAdpter hairAdapter;
    private List<String> bigList;
    private GridView pinpai_view;
    private GridView huapao_view;
    private GridView site_view;
    private List<SlidingSmallBean.brandList> brandLists;
    private List<SlidingSmallBean.siteList> siteList;
    private List<SlidingSmallBean.Paramss> hairLists;
    private EmptyLayout emptyLayout;
    private Context context;
    private Gson gson=new Gson();
    private ImageView img;
    LandsortItemAdpter adpter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_goodslide, null);
        initView(view);
        return view;
    }



    private void initView(View view) {
        context=getActivity();
        goodsort_listview= (ListView) view.findViewById(R.id.goodsort_listview);
        pinpai_view= (GridView) view.findViewById(R.id.pinpai_view);
        huapao_view= (GridView) view.findViewById(R.id.huapao_view);
        site_view= (GridView) view.findViewById(R.id.site_view);
        drawer_layout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
        drawer_content = (RelativeLayout) getActivity().findViewById(R.id.drawer_content);
        goodsort_reset= (TextView) view.findViewById(R.id.goodsort_reset);
        goodsort_sure= (TextView) view.findViewById(R.id.goodsort_sure);
        emptyLayout= (EmptyLayout) view.findViewById(R.id.empty);
        img= (ImageView) view.findViewById(R.id.img_error_layout);
        goodsort_reset.setOnClickListener(this);
        goodsort_sure.setOnClickListener(this);
        img.setOnClickListener(this);
        initDataa();

    }

    private void initDataa() {

//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                showNext(new TowFitrerFragment(),R.id.drawer_content);
//            }
//        });
            emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okUtilCallback=new OkUtilCallback(emptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                SlidingSmallBean bean=gson.fromJson(response,SlidingSmallBean.class);
                if(!bean.getResult().equals("OK")){
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                    BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
                }else{
                            brandLists=bean.getBrandList();
                            siteList=bean.getSiteList();
                            hairLists=bean.getParams();
                    smallAdpter=new SlidingSmallAdpter(context,brandLists);
//                    siteAdapter=new SlidingsiteAdpter(context,siteList);
                    hairAdapter=new SlidinghairAdpter(context,hairLists);

                    pinpai_view.setAdapter(smallAdpter);
                    goodsort_listview.setAdapter(hairAdapter);
//                    site_view.setAdapter(siteAdapter);
                    pinpai_view.setOnItemClickListener(pinpaiListener);
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
//        String params="userId=" +""+"&st="+st;
        OKUtilAPI.post(OKUtilAPI.condition,"", okUtilCallback);


    }

    AdapterView.OnItemClickListener pinpaiListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String value=brandLists.get(i).getName();
            Log.i("yu","value--"+value);
        }
    };



    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.goodsort_reset:
                smallAdpter.clear();
                initDataa();

//                adpter.clear();
//                siteAdapter.clear();
//                hairAdapter.clear();
                break;
            case R.id.goodsort_sure:
                drawer_layout.closeDrawer(drawer_content);
                break;
            case R.id.img_error_layout:
                initDataa();
                break;
        }
    }
}
